<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 添加分类按钮 -->
      <el-row>
        <el-button type="primary" @click="showAddCat">添加分类</el-button>
      </el-row>
      <!-- 商品分类表格 -->
      <tree-table
        :data="goodsCastList"
        :columns="columns"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
        :expand-type="false"
        :selection-type="false"
      >
        <!-- 是否有效组件 -->
        <template slot="isWork" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序组件 -->
        <template slot="order" slot-scope="scope">
          <el-tag effect="dark" v-if="scope.row.cat_level===0" size="mini">一级</el-tag>
          <el-tag type="success" effect="dark" v-else-if="scope.row.cat_level===1" size="mini">二级</el-tag>
          <el-tag type="warning" effect="dark" v-else size="mini">三级</el-tag>
        </template>
        <template slot="operate">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 8, 10]"
        :page-size=" queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCatDialogVisible"
      width="50%"
      @closed="addCaddCateDialogClose"
    >
      <!-- 添加分类表单 -->
      <el-form :model="addCatForm" :rules="addCatRules" ref="addCatRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCatForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- 添加分类级联选择器 -->
          <el-cascader
            v-model="addCatCascaderSelect"
            :options="parentCateList"
            :props="addCatCascaderProps"
            placeholder="请选择"
            clearable
            @change="addCatCascaderChanged"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addCaddCateDialogCencel">取 消</el-button>
        <el-button type="primary" @click="addCateDialogComfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.getGoodsCastList()
  },
  data() {
    return {
      /* 商品分类数据列表 */
      goodsCastList: [],
      /* 页面请求信息 */
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      /* 总共商品条数 */
      total: 0,
      /* 表格列的属性值 */
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isWork'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operate'
        }
      ],
      /* 控制添加分类对话框显示隐藏 */
      addCatDialogVisible: false,
      /* 添加分类表单信息 */
      addCatForm: {
        /* 父级分类ID  */
        cat_pid: 0,
        /* 分类名称 */
        cat_name: '',
        /* 分类层级, 默认添加一级分类 */
        cat_level: 0
      },
      /* 添加分类表单验证规则 */
      addCatRules: {
        cat_pid: [
          { required: true, message: '请选择父级分类', trigger: 'blur' }
        ],
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      /* 添加分类级联选择器选中内容 */
      addCatCascaderSelect: [],
      /* 父级分类信息 */
      parentCateList: [],
      /* 添加分类级联选择器props设置 */
      addCatCascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  methods: {
    /* 获取商品分类数据列表 */
    async getGoodsCastList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类列表失败')
      }
      this.total = res.data.total
      this.goodsCastList = res.data.result
    },
    /* 监听 页数queryInfo.pagenum 发生变化事件 */
    handleSizeChange(newPagesize) {
      this.queryInfo.pagesize = newPagesize
      this.getGoodsCastList()
    },
    /* 监听 页面条数queryInfo.pagesize 发生变化事件 */
    handleCurrentChange(newPagenum) {
      this.queryInfo.pagenum = newPagenum
      this.getGoodsCastList()
    },
    /* 点击添加分类按钮，显示添加添加分类对话框 */
    showAddCat() {
      this.getparentCateList()
      this.parentCateList = this.parentCateList.slice(0, 10)
      this.addCatDialogVisible = true
    },
    /* 获得父级分类信息列表 */
    async getparentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2,
          pagenum: this.queryInfo.pagenum,
          pagesize: this.queryInfo.pagesize
        }
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类列表失败')
      }
      this.parentCateList = res.data.result
    },
    /* 监听 添加分类级联选择器改变 事件 */
    addCatCascaderChanged() {
      if (this.addCatCascaderSelect.length > 0) {
        this.addCatForm.cat_pid = this.addCatCascaderSelect[
          this.addCatCascaderSelect.length - 1
        ]
        this.addCatForm.cat_level = this.addCatCascaderSelect.length
      } else {
        this.addCatForm.cat_pid = 0
        this.addCatForm.cat_level = 0
      }
    },
    /* 点击添加分类对话框确定按钮, 发送请求 */
    addCateDialogComfirm() {
      this.$refs.addCatRef.validate(async valid => {
        if (!valid) {
          return
        }
        console.log(this.addCatForm)
        const { data: res } = await this.$http.post(
          'categories',
          this.addCatForm
        )
        if (res.meta.status !== 201) {
          return this.$message.error('添加商品分类失败')
        }
        this.$message.success('添加商品分类成功')
        this.getGoodsCastList()
        this.addCatDialogVisible = false
        /* 恢复原始值 */
        this.$refs.addCatRef.resetFields()
        this.addCatForm.cat_pid = 0
        this.addCatForm.cat_name = ''
        this.addCatForm.cat_level = 0
        this.addCatCascaderSelect = []
      })
    },
    /* 点击添加分类对话框取消按钮, 复原表单 */
    addCaddCateDialogCencel() {
      this.addCatDialogVisible = false
      this.$refs.addCatRef.resetFields()
      this.addCatForm.cat_pid = 0
      this.addCatForm.cat_name = ''
      this.addCatForm.cat_level = 0
      this.addCatCascaderSelect = []
      console.log(1111)
    },
    /* 监听 添加商品对话框 关闭事件 */
    addCaddCateDialogClose() {
      this.$refs.addCatRef.resetFields()
      this.addCatForm.cat_pid = 0
      this.addCatForm.cat_name = ''
      this.addCatForm.cat_level = 0
      this.addCatCascaderSelect = []
    }
  }
}
</script>

<style lang="scss" scoped>
.zk-table {
  margin-top: 16px;
  margin-bottom: 16px;
}
</style>
